package com.nku.mason.scm.service;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;

import com.nku.mason.scm.dao.AccountMapper;
import com.nku.mason.scm.dao.AccountRecordsMapper;
import com.nku.mason.scm.dao.BaseMapper;
import com.nku.mason.scm.dao.BuyOrderDetailMapper;
import com.nku.mason.scm.dao.BuyOrderMapper;
import com.nku.mason.scm.dao.GoodsMapper;
import com.nku.mason.scm.dao.ReturnOrderMapper;
import com.nku.mason.scm.dao.StoreHouseMapper;
import com.nku.mason.scm.dao.SupplierMapper;
import com.nku.mason.scm.dao.SysParamMapper;
import com.nku.mason.scm.vo.PageBean;

public class BaseServiceImpl<T> implements BaseService<T> {

	@Autowired
	protected SupplierMapper supplierMapper;
	
	@Autowired
	protected AccountMapper accountMapper;
	
	@Autowired
	protected GoodsMapper goodsMapper;
	
	@Autowired
	protected BuyOrderMapper buyOrderMapper;
	
	@Autowired
	protected  BuyOrderDetailMapper buyOrderDetailMapper;
	
	@Autowired
	protected  ReturnOrderMapper returnOrderMapper;
	
//	@Autowired
//	protected  ReturnOrderDetailMapper returnOrderDetailMapper;
	
	@Autowired
	protected StoreHouseMapper storeHouseMapper;
	
	@Autowired
	protected AccountRecordsMapper accountRecordsMapper;
	
	@Autowired
	protected SysParamMapper sysParamMapper;
	
	protected BaseMapper<T> baseMapper;

	@PostConstruct//在构造方法后,初化前执行,完成将子类mapper的类型参数传给BaseMapper的T
	private void initBaseMapper() throws Exception{
		//完成以下逻辑，需要对研发本身进行命名与使用规范
		//this关键字指对象本身，这里指的是调用此方法的实现类（子类）
		System.out.println("=======this :"+this);
		System.out.println("=======父类基本信息："+this.getClass().getSuperclass());
		System.out.println("=======父类和泛型的信息："+this.getClass().getGenericSuperclass());
		
		ParameterizedType type =(ParameterizedType) this.getClass().getGenericSuperclass();
		
		//获取第一个参数的class
		Class clazz = (Class)type.getActualTypeArguments()[0];
		System.out.println("=======class:"+clazz);
		//转化为属性名（相关的Mapper子类的引用名）Supplier --> supplierMapper
		String localField = clazz.getSimpleName().substring(0,1).toLowerCase()+clazz.getSimpleName().substring(1)+"Mapper";
		System.out.println("=======localField:"+localField);
		//getDeclaredField:可以使用于包括私有、默认、受保护、公共字段，但不包括继承的字段
		Field field=this.getClass().getSuperclass().getDeclaredField(localField);
		System.out.println("=======field:"+field);
		System.out.println("=======field对应的对象:"+field.get(this));
		
		Field baseField = this.getClass().getSuperclass().getDeclaredField("baseMapper");
		System.out.println("=======baseField:"+baseField);
		System.out.println("=======baseField对应的对象:"+baseField.get(this));	

		//field.get(this)获取当前this的field字段的值。例如：Supplier对象中，baseMapper所指向的对象为其子类型SupplierMapper对象，子类型对象已被spring实例化于容器中		
		baseField.set(this, field.get(this));		
		System.out.println("========baseField对应的对象:"+baseMapper);

	}	

	
	@Override
	public int insert(T entity) throws Exception {
		return baseMapper.insert(entity);
	}

	@Override
	public int update(T entity) throws Exception {
		return baseMapper.update(entity);
	}

	@Override
	public int delete(T entity) throws Exception {
		return baseMapper.delete(entity);
	}

	@Override
	public int deleteByPks(String[] pks) throws Exception {
		return baseMapper.deleteByPks(pks);
	}

	@Override
	public T select(T entity) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageBean<T> selectPage(PageBean<T> pageBean) throws Exception {
		pageBean.setEntityList(baseMapper.selectPageListDys(pageBean));
		pageBean.setTotalCount(baseMapper.selectTotalCountDys(pageBean));
		return pageBean;
	}

}
